#ifndef MD2LOADER_H
#define MD2LOADER_H

#include "bga/graphics/Loader.h"

#define NORMAL_NUMBER   162

/**
 * \namespace bga
 * \brief API namespace
 */
namespace bga
{

/*!
 * \class MD2Loader
 * \brief
 *
 *
 */
class BOARD_HELL_ENGINE MD2Loader : public GraphicLoader
{
    struct MD2Header
    {
        int ident; ///!< id : "IDP2"
        int version; ///!< file version : 8
        int skinWidth; ///!< width texture
        int skinHeight; ///!< height texture
        int framesize; ///!< size of a frame in bytes
        int numSkins; ///!< number of skins
        int numVertex; ///!< number of vertex per frame
        int numTextureCoordinates; ///!< number of textCoord
        int numTriangles; ///!< number of triangles
        int nuglcmds; ///!< number of GL instructions
        int numFrames; ///!< number de frames
        int offsetSkins; ///!< offset of skin datas
        int offsetTexture; ///!< offset of textCoord datas
        int offsetTriangles; ///!< offset triangle datas
        int offsetFrames; ///!< offset of frame datas
        int offset_glcmds; ///!< offset of GL datas
        int offset_end; ///!< offset of the end of file
    };

    struct MD2Vector
    {
        float vertex[3];
    };

    struct MD2Skin
    {
        char skinFilename[68];    ///!< Name of the texture file used by the skin. The name is stored on 68 bytes
    };

    struct MD2Texture
    {
        short s;
        short t;
    };

    struct MD2Triangle
    {
        unsigned short indexVertex[3];    ///!< List of the index of the vertex
        unsigned short indexTexCoord[3];    ///!< List of the index of the texture coordiantes
    };

    struct MD2Vertex
    {
        unsigned char v[3]; ///!< position of the vertex(relative to model)
        unsigned char normalIndex; ///!< Index of the normal of the vertex
    };

    struct MD2Frame
    {
        float scale[3];   ///!< Scale
        float translation[3];   ///!< Translation
        float name[16];   ///!< Frame name
        MD2Vertex *vertex;   ///!< list of vertex of the frame
    };

	public:
		MD2Loader();
		~MD2Loader();

		virtual void load(const std::string& filename);
		virtual void getDrawable(Drawable& drawable);
        virtual void getTexture(Texture2D& texture);
        virtual void setSceneNode(SceneNode& sceneNode);

	private:
        static MD2Vector normal[NORMAL_NUMBER];

        MD2Header header;
        MD2Skin *skins;
        MD2Texture *textureCoordinates;
        MD2Triangle *triangles;
        MD2Frame *frames;

        void swap(float* array, unsigned char nbElements, unsigned char firstIndex, unsigned char secondIndex);    ///!< Allow to reorganize the array

};


}

#endif // MD2LOADER_H

